<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script>


//【示例】使用递归函数计算fibonacci数列。一个fibonacci数字是之前两个fibonacci数字之和。最前面的两个数字是0和1。
var fibonacci = function(n) { 
    return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
for(var i = 0; i <= 10; i += 1) {
    document.writeln('<br>' + i + ': ' + fibonacci(i));
}

var fibonacci = ( function() {
    var memo = [0, 1];
    var fib = function(n) {
        var result = memo[n];
        if( typeof result !== 'number') {
            result = fib(n - 1) + fib(n - 2);
            memo[n] = result;
        }
        return result;
    };
    return fib;
}());
for(var i = 0; i <= 10; i += 1) {
    document.writeln('<br>' + i + ': ' + fibonacci(i));
}

var memoizer = function(memo, formula) {
    var recur = function(n) {
        var result = memo[n];
        if( typeof result !== 'number') {
            result = formula(recur, n);
            memo[n] = result;
        }
        return result;
    };
    return recur;
};

var fibonacci = memoizer([0, 1], function(recur, n) { 
    return recur(n - 1) + recur(n - 2);
});

var factorial = memoizer([1, 1], function(recur, n) { 
    return n * recur(n - 1);
});


</script>
</head>
<body>




</body>
</html>
